/**
* Author: CZ (cz.devnet@gmail.com)
*
* func StartTiger()
 */

package main

import (
	"fmt"
	"time"
)

var svr *KaoboSvr

//StartSvr start the server
func StartSvr(cfg *KaoboConfig) {
	if svr == nil {
		svr = new(KaoboSvr)
	}
	svr.init(cfg)
	err := svr.start()
	if nil != err {
		println(err.Error())
	}
}

type KaoboSvr struct {
	claw  *Claw
	timer *time.Ticker
}

func (svr *KaoboSvr) init(cfg *KaoboConfig) {
	svr.claw = NewClaw()
	svr.claw.AddURL(config.URL)
	svr.timer = time.NewTicker(5 * time.Second)
}

func (svr *KaoboSvr) start() (err error) {
	if nil == svr {
		fmt.Errorf("Start Kaobo Error with nil svr")
		return ErrNilSvr
	}
	fmt.Printf("[SUCC]:start server successful!\n")
	//Get("http://yjsy.gzucm.edu.cn/zsgz1/bsszs.htm")
	for {
		select {
		case <-svr.timer.C:
			svr.claw.Grab(time.Duration(config.Timer) * time.Second)
		}
	}

	return nil
}
